package com.yunkaweilai.android.activity.login;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.github.lazylibrary.util.StringUtils;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.base.BaseActivity;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.net.HttpRequest;
import com.yunkaweilai.android.net.PostProcess;
import com.yunkaweilai.android.utils.TitleBuilder;
import com.yunkaweilai.android.utils.Utils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 忘记密码
 * Created by Administrator on 2017/7/22.
 */

public class ForgetPassActivity extends BaseActivity {

    @BindView(R.id.id_edt_zh)
    EditText idEdtZh;
    @BindView(R.id.id_edt_phone)
    EditText idEdtPhone;
    @BindView(R.id.id_btn_next)
    Button idBtnNext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forger_pass);
        ButterKnife.bind(this);
        new TitleBuilder(mAc).setTitleText("确认身份")
                .setLeftImage(R.mipmap.ic_go_back)
                .setLeftOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                });
    }

    //验证参数是否为空
    private void isRight() {
        if (StringUtils.isEmpty(idEdtZh.getText().toString())){
            showToast("请输入登录账号");
            return;
        }
        if (StringUtils.isEmpty(idEdtPhone.getText().toString())){
            showToast("请输入手机号码");
            return;
        }else if (idEdtPhone.length() != 11){
            showToast("请输入正确手机号码");
            return;
        }

//        if (StringUtils.isEmpty(idEdtCode.getText().toString())){
//            showToast("请输入验证码");
//            return;
//        }
        check();
//        finish();
//        firstHttp();
    }

//    //第一次提交
//    private void firstHttp(){
//        HttpRequest.postUrl(Api.FORGET_PASSWORD_STP1)
//                .addParams("phone",idEdtPhone.getText().toString())
//                .addParams("UserName",idEdtZh.getText().toString())
//                .addParams("PhoneVerifyCode",idEdtCode.getText().toString())
//                .execute(new PostProcess.StringCallBack() {
//                    @Override
//                    public void onResponse(String json) {
//                        boolean checkCode = Utils.checkCode(mAc, json);
//                        if (checkCode){
//                            Gson gson = new Gson();
//                            ForgetFirstStpModel forgetFirstStpModel = gson.fromJson(json, ForgetFirstStpModel.class);
//                            ForgetPassStp2Activity.actionStart(mAc,forgetFirstStpModel.getData().getForget_password_token());
//                            finish();
//                        }
//                        showLog(json);
//                    }
//
//                    @Override
//                    public void onError(Exception e) {
//
//                    }
//                }
//                );
//    }


    @OnClick({ R.id.id_btn_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.id_btn_next://下一步
                isRight();
                break;
        }
    }

    // 验证
    private void check() {
        HttpRequest.postUrl(Api.ACCOUNT_PASSWORD_CHECK)
                .addParams("UserName",idEdtZh.getText().toString())
                .addParams("Phone",idEdtPhone.getText().toString())
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkCode = Utils.checkCode(mAc, json);
                        if (checkCode){
                            ForgetPassStp2Activity.actionStart(mAc,idEdtZh.getText().toString(),idEdtPhone.getText().toString(),1);
                            finish();
                        }
                    }

                    @Override
                    public void onError(Exception e) {
                        showToast(e.getMessage());
                    }
                });
    }


}
